﻿using System;
using System.Data;
using System.Collections.Generic;
using DB.Common;
using DB.Model;
namespace DB.BLL
{
	/// <summary>
	/// SPort
	/// </summary>
	public partial class SPort
	{
		private readonly DB.DAL.SPort dal=new DB.DAL.SPort();
		public SPort()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			return dal.Exists(ID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(DB.Model.SPort model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DB.Model.SPort model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			return dal.Delete(ID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			return dal.DeleteList(IDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DB.Model.SPort GetModel(int ID)
		{
			
			return dal.GetModel(ID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public DB.Model.SPort GetModelByCache(int ID)
		{
			
			string CacheKey = "SPortModel-" + ID;
			object objModel = DB.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(ID);
					if (objModel != null)
					{
						int ModelCache = DB.Common.ConfigHelper.GetConfigInt("ModelCache");
						DB.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (DB.Model.SPort)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DB.Model.SPort> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DB.Model.SPort> DataTableToList(DataTable dt)
		{
			List<DB.Model.SPort> modelList = new List<DB.Model.SPort>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				DB.Model.SPort model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new DB.Model.SPort();
					if(dt.Rows[n]["ID"]!=null && dt.Rows[n]["ID"].ToString()!="")
					{
						model.ID=int.Parse(dt.Rows[n]["ID"].ToString());
					}
					if(dt.Rows[n]["Name"]!=null && dt.Rows[n]["Name"].ToString()!="")
					{
					model.Name=dt.Rows[n]["Name"].ToString();
					}
					if(dt.Rows[n]["BaudRate"]!=null && dt.Rows[n]["BaudRate"].ToString()!="")
					{
						model.BaudRate=int.Parse(dt.Rows[n]["BaudRate"].ToString());
					}
					if(dt.Rows[n]["Parity"]!=null && dt.Rows[n]["Parity"].ToString()!="")
					{
						model.Parity=int.Parse(dt.Rows[n]["Parity"].ToString());
					}
					if(dt.Rows[n]["DataBits"]!=null && dt.Rows[n]["DataBits"].ToString()!="")
					{
						model.DataBits=int.Parse(dt.Rows[n]["DataBits"].ToString());
					}
					if(dt.Rows[n]["StopBits"]!=null && dt.Rows[n]["StopBits"].ToString()!="")
					{
						model.StopBits=int.Parse(dt.Rows[n]["StopBits"].ToString());
					}
					if(dt.Rows[n]["Encoding"]!=null && dt.Rows[n]["Encoding"].ToString()!="")
					{
						model.Encoding=int.Parse(dt.Rows[n]["Encoding"].ToString());
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

